
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The impact of divorce laws on the equilibrium in the marriage market.
% Reynoso
% April 2024
%
% This file sets up the necessary environment to estimate the model under 
% mutual consent divorce.
%  
% Data: PSID 1968-1992
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------------------ Replication Path -------------------------------%
clc
clear all

%- Indicate location of Replication folder:
%replication_location = 'C:\update_with_your_path';

%------------------------ Preliminaries ----------------------------------%

%---Working directory
cd ([replication_location,'\Internal_estimation\Inputs']);

%--- Path for Data
data_dir = [replication_location,'\Data\Inputs'];

%--- Path for Model
addpath(genpath([replication_location,'\Internal_estimation\Inputs']));

%------------------------ Environment ------------------------------------%
n_g=9;
L_max= 21; 
T=10; 
lt=1; 
dt=3;
H_max=T-lt+1; 
disc=0.98; 
gamma=0.7; 
rho_f_1=1;
rho_f_2=1;
rho_f_3=1;
rho_m_1=1;
rho_m_2=1;
rho_m_3=1;
c_f_1=0;
c_f_2=0;
c_f_3=0;
c_m_1=0;
c_m_2=0;
c_m_3=0;
var_xi_f_1=0.074; 
var_xi_f_2=0.074; 
var_xi_f_3=0.074; 
var_xi_m_1=0.042;
var_xi_m_2=0.042; 
var_xi_m_3=0.042; 
stoch_earn_m_1=[rho_m_1, c_m_1, var_xi_m_1];
stoch_earn_m_2=[rho_m_2, c_m_2, var_xi_m_2];
stoch_earn_m_3=[rho_m_3, c_m_3, var_xi_m_3];
stoch_earn_m=[stoch_earn_m_1;stoch_earn_m_2;stoch_earn_m_3;stoch_earn_m_1;stoch_earn_m_2;stoch_earn_m_3;stoch_earn_m_1;stoch_earn_m_2;stoch_earn_m_3];
stoch_earn_f_1=[rho_f_1, c_f_1, var_xi_f_1];
stoch_earn_f_2=[rho_f_2, c_f_2, var_xi_f_2];
stoch_earn_f_3=[rho_f_3, c_f_3, var_xi_f_3];
stoch_earn_f=[stoch_earn_f_1;stoch_earn_f_1;stoch_earn_f_1;stoch_earn_f_2;stoch_earn_f_2;stoch_earn_f_2;stoch_earn_f_3;stoch_earn_f_3;stoch_earn_f_3];
m_eta_f=2; 
m_eta_m=2;
n_eta_f=10; 
n_eta_m=10;
b0_1=0;
b1_1=0;
b2_1=0;
b3_1=0;
b0_2=0;
b1_2=0;
b2_2=0;
b3_2=0;
b0_3=0;
b1_3=0;
b2_3=0;
b3_3=0;
home_prod_1=[b0_1, b1_1, b2_1, b3_1];
home_prod_2=[b0_2, b1_2, b2_2, b3_2];
home_prod_3=[b0_3, b1_3, b2_3, b3_3];
home_prod=[home_prod_1;home_prod_1;home_prod_1;home_prod_2;home_prod_2;home_prod_2;home_prod_3;home_prod_3;home_prod_3];
match_qual=ones(n_g,2);                    
m_th = 2; 
n_th = 10; 

%------------------------ Optimization structure -------------------------%

%- Bounds
lb_psi=0; 
ub_psi=2;
lb_th1=3.5;
ub_th1=10;
lb_sigma_f=100;
ub_sigma_f=200;
lb_sigma_m=100; 
ub_sigma_m=200;
lb_thvar=0.05; 
ub_thvar=100;
lb_wrate = 7;
ub_wrate = 20;
lb_retexp = 0;
ub_retexp = 0.40;
lb_retexp2 = -0.05; 
ub_retexp2 = 0.1;
lb_retsahw = -0.05; 
ub_retsahw = 0.15;
lb_lambda=0.000001; 
ub_lambda=0.99999;

%- number of parameters to estimate
n_vars=n_g*3 + 6 + 21 + n_g*4 + 1; 

%- Data moments
data_imp = importdata([data_dir,'/data_f.csv']);
data_f = data_imp.data;
data_imp = importdata([data_dir,'/data_m.csv']);
data_m = data_imp.data;
data_imp = importdata([data_dir,'/data_moments.csv']); 
data_imp2 = importdata([data_dir,'/reg_moments.csv']); 
data_moments = [data_imp.data(:,1); data_imp.data(:,2); data_imp.data(:,3); data_imp.data(:,4);...
                data_imp2.data(:,1); data_imp2.data(:,2); data_imp2.data(:,3); data_imp2.data(:,4)];
load([data_dir,'\W.mat']);
load([data_dir,'\X0']);

%- Define problem
problem.solver = 'fminsearch';
problem.objective = @(params) moments(n_g, T, lt, dt, H_max, disc, stoch_earn_f, stoch_earn_m, m_eta_f, m_eta_m, n_eta_f, n_eta_m, home_prod, match_qual, m_th, n_th, gamma, L_max, params, data_moments, data_f, data_m, W);
problem.x0 = X0;
problem.options = optimset('Display','none', 'MaxFunEvals',15000,'MaxIter',3000, 'TolFun', 1e-3, 'TolX',1e-3 );  

%- Save 
save ('inputs_mcd.mat'); 
